//
//  XNWelcomeViewController.swift
//  weibo
//
//  Created by 王向宁 on 16/5/23.
//  Copyright © 2016年 王向宁. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class XNWelcomeViewController: UIViewController {
    
    override func loadView() {
        view = backView
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        setUpUI()
    }
    
    
    private func setUpUI() {
        
        view.addSubview(iconView)
        
        view.addSubview(nameLabel)
        
        //设置约束
        //在初始时候就设置约束  不要在viewDidLayoutSubviews中设置约束
        //系统是根据提前指定好的约束 在viewDidLayoutSubviews方法中根据约束修改frame
        iconView.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.view.snp_centerX)
            
            make.bottom.equalTo(self.view.snp_bottom).offset(-160)
            
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
        
        nameLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(iconView.snp_centerX)
            
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        
        iconView.sd_setImageWithURL(XNUserAccountViewModel.sharedAccountViewModel.headURL)
        
        iconView.cornerRadius = 45
        
        nameLabel.alpha = 0
    }
    
    //动画效果建议放在viewDidApperr阶段
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        startAnimation()
    }
    
    private func startAnimation() {
        
        let offset = -ScreenHeight + 160
        
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 6, options: [], animations: {
            self.iconView.snp_updateConstraints(closure: { (make) in
                make.bottom.equalTo(offset)
            })
            //刷新父视图
            self.view.layoutIfNeeded()
            }) { (_) in
                UIView.animateWithDuration(0.5, animations: { 
                    self.nameLabel.alpha = 1
                    }, completion: { (_) in
                        
//                        UIApplication.sharedApplication().keyWindow?.rootViewController = XNMainViewController()
                        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewController, object: "Main")
                })
                
                
        }
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func prefersStatusBarHidden() -> Bool {
        return true
    
    }
    

    //懒加载子视图
    private lazy var backView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    private lazy var nameLabel: UILabel = UILabel(title: XNUserAccountViewModel.sharedAccountViewModel.userAccount?.name ?? "", font: 18, textColor: UIColor.darkGrayColor())
//    private lazy var nameLabel: UILabel = {
//        let l = UILabel()
//        
//        l.text = XNUserAccountViewModel.sharedAccountViewModel.userAccount?.name
//        
//        l.font = UIFont.systemFontOfSize(18)
//        
//        l.textColor = UIColor.darkGrayColor()
//        
//        l.sizeToFit()
//        
//        l.alpha = 0
//        
//        return l
//    }()

}
